package com.xzx.spark.core.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 *
 *
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2021-06-26 8:39 下午
 */
object Spark019_KeyValue_FoldByKey {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[*]").setAppName("Spark019_KeyValue_FoldByKey")
    val context = new SparkContext(conf)
    //分区间和分区内相加
    val rdd: RDD[(String, Int)] = context.makeRDD(List(("a", 1), ("a", 2), ("c", 3), ("b", 4), ("c", 5), ("c", 6)), 2)
    val result: RDD[(String, Int)] = rdd.foldByKey(0)(_ + _)
    result.collect().foreach(println)

    context.stop()
  }
}
